<?php
namespace App\Services\Exchangerate;
use App\Services\Exchangerate\Exchangerate\ExchangerateLogic;
use App\Services\Manage\ManageIndex;
use App\Services\User\Token;
/**
 * Class Exchangerate 汇率服务
 * @package App\Services\Exchangerate
 */
class Exchangerate
{
    /*添加一条汇率记录 注；若只传一个价格，则所有价格相同(仅适用于添加功能！)*/
    public function addRate($data): array 
    {

        $userInfo = (new Token())->hGetAll($data['token']);
        if($userInfo['res']['status'] != SUCCESS_STATUS){
            return $userInfo;
        }
        $data['manage_id'] = $userInfo['res']['id'];
         //若只传一个值，则五个值为一个价格
        if(empty($data['max_price']) + empty($data['min_price']) + empty($data['open_price']) + empty($data['close_price']) + empty($data['now_price']) == 4){
            $data['max_price'] = $data['min_price'] = $data['open_price'] = $data['close_price'] = $data['now_price'] = array_sum($data);
        }
        unset($data['token']);
        if($data['max_price'] < $data['min_price']){    return verify_err(null, '最高价不能小于最低价');  }
        if($data['open_price'] < $data['min_price'] || $data['open_price'] > $data['max_price']){ return verify_err(null, '开盘价应在最低最高价之间'); }
        if($data['close_price'] < $data['min_price'] || $data['close_price'] > $data['max_price']){ return verify_err(null, '收盘价应在最低最高价之间'); }
        if($data['now_price'] < $data['min_price'] || $data['now_price'] > $data['max_price']){ return verify_err(null, '当前价应在最低最高价之间'); }
        $data = init_array($data, ['manage_id'=>0, 'max_price'=>0, 'min_price'=>0, 'open_price'=>0, 'close_price'=>0, 'now_price'=>0]);
        /*var_dump($data);
        exit;*/
        ValidateExchangerate::checkValidate($data, ['manage_id','max_price','min_price','open_price','close_price','now_price']);
        return (new ExchangerateLogic())->addRate($data);

    }

    /*获取汇率记录列表（分页数据）*/
    public function rateList($data): array 
    {
        unset($data['token']);
        $data = init_array($data, ['page'=>1,'listRows'=>15]);
        ValidateExchangerate::checkValidate($data, ['page','listRows']);
        return (new ExchangerateLogic())->rateList($data);
    }

    /*修改其中一条记录*/
    public function updateRate($data): array 
    {
        $userInfo = (new Token())->hGetAll($data['token']);
        if($userInfo['res']['status'] != SUCCESS_STATUS){
            return $userInfo;
        }
        $data['manage_id'] = $userInfo['res']['id'];
        unset($data['token']);
        if($data['max_price'] < $data['min_price']){    return verify_err(null, '最高价不能小于最低价');  }
        if($data['open_price'] < $data['min_price'] || $data['open_price'] > $data['max_price']){ return verify_err(null, '开盘价应在最低最高价之间'); }
        if($data['close_price'] < $data['min_price'] || $data['close_price'] > $data['max_price']){ return verify_err(null, '收盘价应在最低最高价之间'); }
        if($data['now_price'] < $data['min_price'] || $data['now_price'] > $data['max_price']){ return verify_err(null, '当前价应在最低最高价之间'); }
        $data = init_array($data, ['id'=>0,'manage_id'=>0, 'max_price'=>0, 'min_price'=>0, 'open_price'=>0, 'close_price'=>0, 'now_price'=>0]);
        ValidateExchangerate::checkValidate($data, ['id','manage_id','max_price','min_price','open_price','close_price','now_price']);
        return (new ExchangerateLogic())->updateRate($data);
    }

    /*获取最新一条汇率记录*/
  /*  public function oneNewRate($data): array 
    {
        return $this->stepRun([], 'oneNewRate', '', $this->exchangerateValidate, $this->exchangerateLogic);
    }*/

    /*删除一条汇率记录*/
    public function delOneRate($data): array 
    {
        unset($data['token']);
        $data = init_array($data, ['id'=>0]);
        ValidateExchangerate::checkValidate($data, ['id']);
        return (new ExchangerateLogic())->delOneRate($data);
    }




}